package com.wms.security;

import com.alibaba.druid.support.json.JSONUtils;
import com.wms.controller.admin.BaseController;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;utf-8");
        response.getWriter().write(JSONUtils.toJSONString(BaseController.unauthorize()));
    }

    public void onPermissionDenied(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;utf-8");
        response.getWriter().write(JSONUtils.toJSONString(BaseController.unPermission()));
    }

}
